package com.deliveryservicesystem.presentation.desktop.report;

import com.deliveryservicesystem.app.exceptions.OutputError;
import com.deliveryservicesystem.app.util.AppUtils;
import com.deliveryservicesystem.dataaccess.PersistenceDbManager;
import com.deliveryservicesystem.model.Customer;
import com.deliveryservicesystem.model.Driver;
import com.deliveryservicesystem.model.Supplier;
import com.deliveryservicesystem.presentation.desktop.MatchDialog;
import com.deliveryservicesystem.service.impl.Report1Impl;
import com.deliveryservicesystem.service.impl.Report2Impl;
import com.deliveryservicesystem.service.impl.Report3Impl;
import com.toedter.calendar.JDateChooser;
import java.awt.Component;
import java.awt.Font;
import java.awt.print.PrinterException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;

/**
 * File Name: ReportFrame.java
 * Version:
 * Description:
 * Package: com.deliveryservicesystem.presentation.desktop
 * Author: Lance
 * Date: Jul 26, 2012
 * Editor:
 */
public class ReportFrame extends javax.swing.JFrame
{

    private static final long serialVersionUID = 1L;
    private PersistenceDbManager dbManager;
    private final JDateChooser fromDateChooser;
    private final JDateChooser toDateChooser;

    /**
     * Creates new form ReportFrame
     */
    @SuppressWarnings("deprecation")
    public ReportFrame()
    {
        dbManager = PersistenceDbManager.getInstance();
        String dateFormatConverter = AppUtils.toLocaleDate(new Date(), "yyyy-MM-dd", Locale.ENGLISH);
        fromDateChooser = new JDateChooser(null, new Date(), null, new com.toedter.calendar.JSpinnerDateEditor());
        fromDateChooser.getJCalendar().setTodayButtonVisible(true);
        toDateChooser = new JDateChooser(null, new Date(), null, new com.toedter.calendar.JSpinnerDateEditor());
        toDateChooser.getJCalendar().setTodayButtonVisible(true);
        fromDateChooser.setDate(java.sql.Date.valueOf(dateFormatConverter));
        toDateChooser.setDate(java.sql.Date.valueOf(dateFormatConverter));

        initComponents();
        cbDriverNo.setSelectedIndex(-1);
        cbCustNo.setSelectedIndex(-1);
        cbSupplierNo.setSelectedIndex(-1);
        AppUtils.locateOnScreenCenter(this);
        setVisible(true);
        setTitle("Run Report");
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }

    private void runReoprt1()
    {
        try
        {
            Driver d = (Driver) cbDriverNo.getSelectedItem();
            Report1Impl service = new Report1Impl();
            ResultSet deliveryDriver = service.getDeliveryDriver(d.getDriverNo().toString(), fromDateChooser.getDate(), toDateChooser.getDate());
            String formatResultSet = service.formatResultSet(deliveryDriver);
            reportEditorPane.setText("Report date: " + new Date() + "\n\nDriver Details: \n====================" + d.toString() + "\n\n" + formatResultSet);
            //reportEditorPane.setText("\n" + formatResultSet);
        }
        catch (NullPointerException ex)
        {
            AppUtils.showUserMessage(rootPane, "Please select a driver", "Select driver", JOptionPane.INFORMATION_MESSAGE);
            cbDriverNo.requestFocus();
        }
        catch (SQLException | ParseException ex)
        {
            OutputError.toLog(ex);
            OutputError.toUser(ex);
        }
    }

    private void runReoprt2()
    {
        try
        {
            Customer c = (Customer) cbCustNo.getSelectedItem();
            Report2Impl reportTwo = new Report2Impl();
            String customerProducts = reportTwo.getCustomerProducts(c.getCustomerNo().toString());
            reportEditorPane.setText("Report date: " + new Date() + "\n\nCustomer Details: \n====================\n" + c.toString() + 
                    "\n\nCustomer Products\n====================\n" + customerProducts);
        }
        catch (NullPointerException ex)
        {
            AppUtils.showUserMessage(rootPane, "Please select a customer", "Select customer", JOptionPane.INFORMATION_MESSAGE);
            cbCustNo.requestFocus();
        }
    }

    private void runReoprt3()
    {
        try
        {
            Supplier s = (Supplier) cbSupplierNo.getSelectedItem();
            Report3Impl report3Impl = new Report3Impl();
            String customerProducts = report3Impl.getSupplierProducts(s.getSupplierNo().toString());
            reportEditorPane.setText("Report date: " + new Date() + "\n\nSupplier Details: \n====================\n" + s.toString() + 
                    "\n\nSupplier Products\n====================\n" + customerProducts);
        }
        catch (NullPointerException ex)
        {
            AppUtils.showUserMessage(rootPane, "Please select a supplier", "Select supplier", JOptionPane.INFORMATION_MESSAGE);
            cbCustNo.requestFocus();
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = dbManager.getEntityManager();
        driverQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT d FROM Driver d");
        driverList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : driverQuery.getResultList();
        customerQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Customer c");
        customerList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : customerQuery.getResultList();
        supplierQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT s FROM Supplier s");
        supplierList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : supplierQuery.getResultList();
        lblStatusBar = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        reportTabbedPane = new javax.swing.JTabbedPane();
        delDriPanel = new javax.swing.JPanel();
        lblDriverNo = new javax.swing.JLabel();
        cbDriverNo = new javax.swing.JComboBox();
        btnMatch = new javax.swing.JButton();
        lblDeliveryDate = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtFromDate = new javax.swing.JTextField();
        txtToDate = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        cbCustNo = new javax.swing.JComboBox();
        btnMatchCustomer = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        cbSupplierNo = new javax.swing.JComboBox();
        btnMatchSupplier = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnRun = new javax.swing.JButton();
        btnPrint = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        reportEditorPane = new javax.swing.JEditorPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblStatusBar.setText("lblStatusBar");

        reportTabbedPane.setTabPlacement(javax.swing.JTabbedPane.LEFT);
        reportTabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                reportTabbedPaneStateChanged(evt);
            }
        });

        lblDriverNo.setText("<html><b>Step 1: Select Driver Number</b></html>");

        cbDriverNo.setRenderer(new DefaultListCellRenderer()
            {

                @Override
                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
                {
                    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    if (value instanceof Driver)
                    {
                        Driver driver = (Driver) value;
                        setText(driver.getDriverNo().toString());
                    }
                    return this;
                }
            });

            org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, driverList, cbDriverNo, "driverno");
            bindingGroup.addBinding(jComboBoxBinding);
            org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, driverList, org.jdesktop.beansbinding.ObjectProperty.create(), cbDriverNo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "driver");
            bindingGroup.addBinding(binding);

            btnMatch.setText("Match");
            btnMatch.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnMatchActionPerformed(evt);
                }
            });

            lblDeliveryDate.setText("<html><b>Step 2: Select Delivery Date</b></html>");

            jLabel1.setText("From");

            txtFromDate.setText("Date");

            txtToDate.setText("Date");

            jLabel2.setText("To");

            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(jLabel2))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(toDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(fromDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(fromDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(toDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(43, Short.MAX_VALUE))
            );

            javax.swing.GroupLayout delDriPanelLayout = new javax.swing.GroupLayout(delDriPanel);
            delDriPanel.setLayout(delDriPanelLayout);
            delDriPanelLayout.setHorizontalGroup(
                delDriPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, delDriPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(delDriPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblDriverNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(delDriPanelLayout.createSequentialGroup()
                            .addComponent(cbDriverNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnMatch))
                        .addGroup(delDriPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblDeliveryDate, javax.swing.GroupLayout.Alignment.LEADING)))
                    .addContainerGap(59, Short.MAX_VALUE))
            );
            delDriPanelLayout.setVerticalGroup(
                delDriPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(delDriPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(lblDriverNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(delDriPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cbDriverNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnMatch))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(lblDeliveryDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(228, Short.MAX_VALUE))
            );

            reportTabbedPane.addTab("Delivery/Driver", delDriPanel);

            jLabel11.setText("<html><b>Step 1: Select Customer Number</b></html>");

            cbCustNo.setRenderer(new DefaultListCellRenderer()
                {

                    @Override
                    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
                    {
                        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                        if (value instanceof Customer)
                        {
                            Customer customer = (Customer) value;
                            setText(customer.getCustomerNo().toString());
                        }
                        return this;
                    }
                });

                jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, customerList, cbCustNo, "custno");
                bindingGroup.addBinding(jComboBoxBinding);
                binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, customerList, org.jdesktop.beansbinding.ObjectProperty.create(), cbCustNo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "cust");
                bindingGroup.addBinding(binding);

                btnMatchCustomer.setText("Match");
                btnMatchCustomer.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        btnMatchCustomerActionPerformed(evt);
                    }
                });

                javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
                jPanel4.setLayout(jPanel4Layout);
                jPanel4Layout.setHorizontalGroup(
                    jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(cbCustNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnMatchCustomer)))
                        .addContainerGap(39, Short.MAX_VALUE))
                );
                jPanel4Layout.setVerticalGroup(
                    jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbCustNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnMatchCustomer))
                        .addContainerGap(359, Short.MAX_VALUE))
                );

                reportTabbedPane.addTab("Customer Products", jPanel4);

                cbSupplierNo.setRenderer(new DefaultListCellRenderer()
                    {

                        @Override
                        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
                        {
                            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                            if (value instanceof Supplier)
                            {
                                Supplier supplier = (Supplier) value;
                                setText(supplier.getSupplierNo().toString());
                            }
                            return this;
                        }
                    });

                    jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, supplierList, cbSupplierNo, "supplier");
                    bindingGroup.addBinding(jComboBoxBinding);
                    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, supplierList, org.jdesktop.beansbinding.ObjectProperty.create(), cbSupplierNo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "supplierno");
                    bindingGroup.addBinding(binding);

                    btnMatchSupplier.setText("Match");
                    btnMatchSupplier.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btnMatchSupplierActionPerformed(evt);
                        }
                    });

                    jLabel12.setText("<html><b>Step 1: Select Supplier Number</b></html>");

                    javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
                    jPanel5.setLayout(jPanel5Layout);
                    jPanel5Layout.setHorizontalGroup(
                        jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel5Layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel5Layout.createSequentialGroup()
                                    .addComponent(cbSupplierNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnMatchSupplier))
                                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addContainerGap(48, Short.MAX_VALUE))
                    );
                    jPanel5Layout.setVerticalGroup(
                        jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel5Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cbSupplierNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnMatchSupplier))
                            .addContainerGap(359, Short.MAX_VALUE))
                    );

                    reportTabbedPane.addTab("Supplier Products", jPanel5);

                    jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Controles"));

                    btnRun.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/general/Find16.gif"))); // NOI18N
                    btnRun.setText("RUN!");
                    btnRun.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btnRunActionPerformed(evt);
                        }
                    });

                    btnPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/general/Print16.gif"))); // NOI18N
                    btnPrint.setText("Print");
                    btnPrint.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btnPrintActionPerformed(evt);
                        }
                    });

                    btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/general/Stop16.gif"))); // NOI18N
                    btnExit.setText("Exit");
                    btnExit.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btnExitActionPerformed(evt);
                        }
                    });

                    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
                    jPanel2.setLayout(jPanel2Layout);
                    jPanel2Layout.setHorizontalGroup(
                        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(btnRun, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnPrint, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    );
                    jPanel2Layout.setVerticalGroup(
                        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btnRun)
                                .addComponent(btnPrint)
                                .addComponent(btnExit))
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    );

                    getRootPane().setDefaultButton(btnRun);

                    reportEditorPane.setFont(new Font("Serif", Font.ITALIC, 16));
                    jScrollPane1.setViewportView(reportEditorPane);

                    jMenu1.setText("File");

                    jMenuItem1.setText("jMenuItem1");
                    jMenu1.add(jMenuItem1);

                    jMenuBar1.add(jMenu1);

                    jMenu2.setText("Edit");

                    jMenuItem2.setText("jMenuItem2");
                    jMenu2.add(jMenuItem2);

                    jMenuBar1.add(jMenu2);

                    setJMenuBar(jMenuBar1);

                    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                    getContentPane().setLayout(layout);
                    layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(0, 0, Short.MAX_VALUE)
                                    .addComponent(lblStatusBar))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(reportTabbedPane))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 539, Short.MAX_VALUE)))
                            .addContainerGap())
                    );
                    layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(reportTabbedPane)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jScrollPane1))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lblStatusBar))
                    );

                    bindingGroup.bind();

                    pack();
                }// </editor-fold>//GEN-END:initComponents

    private void btnPrintActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnPrintActionPerformed
    {//GEN-HEADEREND:event_btnPrintActionPerformed
        try
        {
            reportEditorPane.print();
        }
        catch (PrinterException ex)
        {
            OutputError.toLog(ex);
            OutputError.toUser(ex);
        }
    }//GEN-LAST:event_btnPrintActionPerformed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnExitActionPerformed
    {//GEN-HEADEREND:event_btnExitActionPerformed
        if (AppUtils.askExitConfirm() == JOptionPane.YES_OPTION)
        {
            dispose();
        }
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnMatchActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnMatchActionPerformed
    {//GEN-HEADEREND:event_btnMatchActionPerformed
        try
        {
            MatchDialog frame = new MatchDialog(null, true, "BEGIN ? := GET_DATA.DRIVERS; END;");
            AppUtils.locateOnScreenCenter(frame);
            frame.setVisible(true);
            cbDriverNo.setSelectedItem(new Driver(frame.getReturnStatus()));
        }
        catch (SQLException ex)
        {
            OutputError.toLog(ex);
            OutputError.toUser(ex);
        }
    }//GEN-LAST:event_btnMatchActionPerformed

    private void btnRunActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnRunActionPerformed
    {//GEN-HEADEREND:event_btnRunActionPerformed
        if (reportTabbedPane.getSelectedIndex() == Tab.REPORT1.getCode())
        {
            runReoprt1();
        }
        else if (reportTabbedPane.getSelectedIndex() == Tab.REPORT2.getCode())
        {
            runReoprt2();
        }
        else if (reportTabbedPane.getSelectedIndex() == Tab.REPORT3.getCode())
        {
            runReoprt3();
        }
    }//GEN-LAST:event_btnRunActionPerformed

    private void reportTabbedPaneStateChanged(javax.swing.event.ChangeEvent evt)//GEN-FIRST:event_reportTabbedPaneStateChanged
    {//GEN-HEADEREND:event_reportTabbedPaneStateChanged
        reportEditorPane.setText("Complete selection and hit enter to run the report");
    }//GEN-LAST:event_reportTabbedPaneStateChanged

    private void btnMatchCustomerActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnMatchCustomerActionPerformed
    {//GEN-HEADEREND:event_btnMatchCustomerActionPerformed
        try
        {
            MatchDialog frame = new MatchDialog(null, true, "BEGIN ? := GET_DATA.CUSTOMERS; END;");
            AppUtils.locateOnScreenCenter(frame);
            frame.setVisible(true);
            cbCustNo.setSelectedItem(new Customer(frame.getReturnStatus()));
        }
        catch (SQLException ex)
        {
            OutputError.toLog(ex);
            OutputError.toUser(ex);
        }
    }//GEN-LAST:event_btnMatchCustomerActionPerformed

    private void btnMatchSupplierActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnMatchSupplierActionPerformed
    {//GEN-HEADEREND:event_btnMatchSupplierActionPerformed
        try
        {
            MatchDialog frame = new MatchDialog(null, true, "BEGIN ? := GET_DATA.SUPPLIERS; END;");
            AppUtils.locateOnScreenCenter(frame);
            frame.setVisible(true);
            cbSupplierNo.setSelectedItem(new Supplier(frame.getReturnStatus()));
        }
        catch (SQLException ex)
        {
            OutputError.toLog(ex);
            OutputError.toUser(ex);
        }
    }//GEN-LAST:event_btnMatchSupplierActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnMatch;
    private javax.swing.JButton btnMatchCustomer;
    private javax.swing.JButton btnMatchSupplier;
    private javax.swing.JButton btnPrint;
    private javax.swing.JButton btnRun;
    private javax.swing.JComboBox cbCustNo;
    private javax.swing.JComboBox cbDriverNo;
    private javax.swing.JComboBox cbSupplierNo;
    private java.util.List<com.deliveryservicesystem.model.Customer> customerList;
    private javax.persistence.Query customerQuery;
    private javax.swing.JPanel delDriPanel;
    private java.util.List<com.deliveryservicesystem.model.Driver> driverList;
    private javax.persistence.Query driverQuery;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblDeliveryDate;
    private javax.swing.JLabel lblDriverNo;
    private javax.swing.JLabel lblStatusBar;
    private javax.swing.JEditorPane reportEditorPane;
    private javax.swing.JTabbedPane reportTabbedPane;
    private java.util.List<com.deliveryservicesystem.model.Supplier> supplierList;
    private javax.persistence.Query supplierQuery;
    private javax.swing.JTextField txtFromDate;
    private javax.swing.JTextField txtToDate;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
